package com.gxa.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.admin.exception.SystemException;
import com.gxa.admin.mapper.CategoryMapper;
import com.gxa.admin.param.UserParam;
import com.gxa.admin.service.CategoryService;
import com.gxa.o2o.core.dto.ResultDTO;
import com.gxa.o2o.core.pojo.Category;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(value = "categoryService")
@Transactional(rollbackFor = Throwable.class)
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;

    /**
     * 查询所有分类（未被删除的）
     * @return
     */
    @Override
    public ResultDTO listAll() {
        List<Category> categories = categoryMapper.listAll();
        return new ResultDTO(200,"查询成功",categories);
    }

    /**
     * 通过条件分页查询
     * @return
     */
    @Override
    public ResultDTO listByParam(UserParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        List<Category> categories = categoryMapper.listByParam(param);
        PageInfo<Category> pageInfo = PageInfo.of(categories);
        return new ResultDTO(200,"查询成功",pageInfo);
    }

    /**
     * 通过删除（逻辑删除）
     * @param ids
     * @return
     */


    @Override
    public ResultDTO delete(List<Integer> ids) {
        List<Integer> idList = new ArrayList<>();
        if (categoryMapper.updateDeleted(ids) != ids.size()) {
            throw new SystemException(1007,"删除失败");
        }else{
            return new ResultDTO(200,"删除成功");
        }

    }

    @Override
    public ResultDTO updateOne(Category category) {
        if(category.getCategoryId() == null || category.getCategoryName() ==null){
            return new ResultDTO(1001,"id或类别名不能为空");
        }
        if(category.getCategoryId() <= 0){
            return new ResultDTO(1001,"id错误");
        }
        //查询数据库
        Category dbCategory = categoryMapper.selectByPrimaryKey(category.getCategoryId());
        if(dbCategory ==null) {
            return new ResultDTO(1001, "该类别不存在");
        }
        if(dbCategory.getCategoryIsDeleted() == 1){
            return new ResultDTO(1001,"该类别已删除，不能修改");
        }
        //修改数据库
        if (categoryMapper.updateByPrimaryKeySelective(category) == 1) {
            return new ResultDTO(200,"修改成功");
        }else{
          throw new SystemException(1001,"修改失败");
        }
    }

    @Override
    public ResultDTO add(Category category) {
        //查询数据库（已有的类别不能重复添加，排除已被删除的类别）
        List<Category> dbCategory = categoryMapper.findByCategoryName(category.getCategoryName());
        if(dbCategory != null && dbCategory.size() >= 1){
            return new ResultDTO(1001,"该类别已存在");
        }


        if (categoryMapper.insertSelective(category) == 1) {
            return new ResultDTO(200,"添加成功");
        }
        return new ResultDTO(1001,"添加失败");
    }
}
